package pe.com.bn.sach.mantenimiento.controller;


import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pe.com.bn.sach.seguridad.DatosSesion;
import pe.com.bn.sach.service.MantenimientoService;
import pe.com.bn.sach.service.SelectService;

import pe.com.bn.sach.mantenimiento.form.GiroForm;


//import pe.com.bn.sach.domain.Bnchf08Ubigeo;
import pe.com.bn.sach.domain.Bnchf07Item;
import pe.com.bn.sach.domain.Bnchf07ItemId;
import pe.com.bn.sach.domain.Bnchf27GiroActividad;

import java.util.Map;
import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class GiroController extends MultiActionController {
	
    private String buscar;
    private String consultar;
    
    private MantenimientoService servicioMantenimiento;
    SelectService servicioSelect;
    private String commandName;
    private Class commandClass;
    /** Logger for this class and subclasses */    
    protected final Log logger = LogFactory.getLog(getClass());
    
    public GiroController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("buscarGiro");
        setMethodNameResolver(resolver);
    }

    public ModelAndView buscarGiro(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	//ModelAndView mav = new ModelAndView(getConsultar());
    	Bnchf27GiroActividad bnchf27Giro;
    	bnchf27Giro=new Bnchf27GiroActividad();
    	
    	//List lis=servicioMantenimiento.buscarGiro(bnchf27Giro);    	
      		
		Map myModel = new HashMap();
		//myModel.put("listGiro",servicioMantenimiento.buscarGiro(bnchf27Giro));
		ModelAndView mav = new ModelAndView(getConsultar(),"model",myModel);
		return mav;
    }
    public ModelAndView busquedaGiroAjax(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		ModelAndView mav = new ModelAndView("mantenimiento/buscarGiroAjax");
		
		GiroForm form = new GiroForm();  
		Object command = getCommandObject(request);
		form = (GiroForm)command;
		Bnchf27GiroActividad bnchf27GiroActividad= new Bnchf27GiroActividad();
		bnchf27GiroActividad.setF27DescGiro(form.getTxtDescGiro());
		if(form.getTxtCodGiro()!=null && !form.getTxtCodGiro().equals("")) 
			bnchf27GiroActividad.setF27IdGiro(new Long(form.getTxtCodGiro()));
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("lstGiro",servicioMantenimiento.busquedaGiro(bnchf27GiroActividad));
		mav.addObject("idGiro",form.getTxtCodGiro());
		mav.addObject("descGiro",form.getTxtDescGiro());
		return mav;
    }    
    
    public ModelAndView listarGiros(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/buscarGiro");
		
		GiroForm form = new GiroForm();
		Object command = getCommandObject(request);
		form = (GiroForm)command;

		Bnchf27GiroActividad bnchf27GiroActividad= new Bnchf27GiroActividad();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		bnchf27GiroActividad.setF27DescGiro("");
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("lstGiro",servicioMantenimiento.listarGiro(bnchf27GiroActividad));
		return mav;
    }
    
    public ModelAndView nuevoGiro(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/registrarGiro");
		
		GiroForm form = new GiroForm();
		Object command = getCommandObject(request);
		form = (GiroForm)command;
	
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		
		return mav;
    }
   
    
    public ModelAndView guardarGiro(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		
		GiroForm form = new GiroForm();
		Object command = getCommandObject(request);
		form = (GiroForm)command;
		DatosSesion datosSesion = new DatosSesion();
		datosSesion = (DatosSesion)request.getSession().getAttribute("datosSesion");
		String estado = form.getSelEstado();
		String descGiro =form.getTxtGiro();
		Bnchf27GiroActividad bnchf27GiroActividad= new Bnchf27GiroActividad();
		bnchf27GiroActividad.setF27DescGiro(descGiro.toUpperCase());
		bnchf27GiroActividad.setF27StdoGiro(new Long(estado));
		bnchf27GiroActividad.setF27IdUsuaCrea(datosSesion.getCodigoHost());
		servicioMantenimiento.guardarGiro(bnchf27GiroActividad);
		return this.listarGiros(request,response);
    }
    public ModelAndView modificarGiro(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/actualizarGiro");
		
		GiroForm form = new GiroForm();
		Object command = getCommandObject(request);
		form = (GiroForm)command;
		String estado = form.getSelEstado();
		String descGiro =form.getTxtGiro();
		String idGiro =form.getTxtIdGiro();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("descGiro", descGiro);
		mav.addObject("idGiro", idGiro);
		mav.addObject("estado", estado);
		
		return mav;
    }
   
    
    public ModelAndView actualizarGiro(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		
		GiroForm form = new GiroForm();
		Object command = getCommandObject(request);
		form = (GiroForm)command;
		DatosSesion datosSesion = new DatosSesion();
		datosSesion = (DatosSesion)request.getSession().getAttribute("datosSesion");
		String estado = form.getSelEstado();
		String descGiro =form.getTxtGiro();
		String idGiro =form.getTxtIdGiro();
		Bnchf27GiroActividad bnchf27GiroActividad= new Bnchf27GiroActividad();
		bnchf27GiroActividad.setF27DescGiro(descGiro.toUpperCase());
		bnchf27GiroActividad.setF27IdGiro(new Long(idGiro));
		bnchf27GiroActividad.setF27StdoGiro(new Long(estado));
		bnchf27GiroActividad.setF27IdUsuaModi(datosSesion.getCodigoHost());
		servicioMantenimiento.actualizarGiro(bnchf27GiroActividad);
		return this.listarGiros(request,response);
    }
    
    public ModelAndView consultarGiro(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/consultarGiro");
		
		GiroForm form = new GiroForm();
		Object command = getCommandObject(request);
		form = (GiroForm)command;
		String idGiro = form.getTxtIdGiro();
		String estado = form.getSelEstado();
		String descGiro = form.getTxtGiro();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("descGiro", descGiro);
		mav.addObject("idGiro", idGiro);
		mav.addObject("estado", estado);
		
		return mav;
    }
    
    
    
    
   public Object getCommandObject(HttpServletRequest request)
    throws Exception
    {
    Object command = formBackingObject(request);
    bind(request, command);
    return command;
    }
    protected Object formBackingObject(HttpServletRequest request)
    throws Exception
    {
        return commandClass.newInstance();
    }

	public void setServicioMantenimiento(MantenimientoService servicioMantenimiento) {
		this.servicioMantenimiento = servicioMantenimiento;
	}


	public Class getCommandClass() {
		return commandClass;
	}


	public void setCommandClass(Class commandClass) {
		this.commandClass = commandClass;
	}


	public String getCommandName() {
		return commandName;
	}


	public void setCommandName(String commandName) {
		this.commandName = commandName;
	}

	public String copyString(String vari){
		if(vari == null || vari == "null" )
			vari="";
		return vari;
		
	}
	
	
	public int copyInt(String vari){
		if(vari == null || vari == "null" )
			vari="0";
		return Integer.parseInt(vari);
		
		
	}
	
	public java.util.Date copyDate(String vari) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date d = null;
		if(vari == null || vari == "null" )
			vari ="10-10-2000";
			d=sdf.parse(vari);
		return d;
		
	}


	public String getBuscar() {
		return buscar;
	}


	public void setBuscar(String buscar) {
		this.buscar = buscar;
	}

	public String getConsultar() {
		return consultar;
	}

	public void setConsultar(String consultar) {
		this.consultar = consultar;
	}


	
	/**
	 * @param servicioSelect El servicioSelect a establecer.
	 */
	public void setServicioSelect(SelectService servicioSelect) {
		this.servicioSelect = servicioSelect;
	}
}



